package com.sunjob.mroresource.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunjob.common.domain.resource.Staff;
import com.sunjob.common.domain.resource.StaffPosition;
import com.sunjob.common.domain.resource.StaffTitle;
import com.sunjob.common.domain.resource.StaffType;
import com.sunjob.common.result.Result;
import com.sunjob.mroresource.mapper.StaffMapper;
import com.sunjob.mroresource.service.StaffService;
import com.sunjob.mroresource.util.ResourceUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.lang.NonNullApi;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: TomHuang
 * @date: 2022/8/11 16:10
 */

@RestController
@Api(tags = "人员管理")
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private StaffService staffService;

    @GetMapping("/find/id/{StaffId}")
    @ResponseBody
    @ApiOperation(value = "根据员工id查询员工相关信息")
    public Result<Staff> getStaffById(@ApiParam(value = "员工id",required = true) @PathVariable Long StaffId){
        Staff staff=staffService.selectStaffById(StaffId);
        if(staff==null){
            return Result.fail("该员工不存在！");
        }
        //staff.setStaffGender(staff.getStaffGender()== 0 ?"女":"男");
        return Result.ok(staff,"查询员工成功！");
    }

    @GetMapping("/find/jobId/{StaffJobId}")
    @ResponseBody
    @ApiOperation(value = "根据员工工号查询员工相关信息")
    public Result<Staff> getStaffByJobId(@ApiParam(value = "员工工号",required = true) @PathVariable String StaffJobId){

        Staff staff=staffService.selectStaffByStaffJobId(StaffJobId);
        if(staff==null){
            return Result.fail("该员工不存在！");
        }
        return Result.ok(staff,"查询员工成功！");
    }

    @GetMapping("/find/name/{StaffName}/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "根据员工名字查询员工相关信息")
    public Result<PageInfo> getStaffByName(@ApiParam(value = "员工姓名",required = true) @PathVariable String StaffName,
                                           @ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<Staff> staffs=staffService.selectStaffByStaffName(StaffName);
        if(staffs.size()==0){
            return Result.fail("该员工不存在！");
        }
        PageInfo pageInfo=new PageInfo(staffs);
        return Result.ok(pageInfo,"查询员工成功！");
    }

    /*@GetMapping("/idOrName/{StaffId}/{StaffJobId}/{StaffName}")
    @ResponseBody
    @ApiOperation(value = "根据员工Id、工号或者姓名查询员工相关信息")
    public Result<Object> getStaffByIdOrName(@ApiParam(value = "员工id") @PathVariable(required = false) String StaffId,
                                            @ApiParam(value = "员工工号") @PathVariable(required = false) String StaffJobId,
                                            @ApiParam(value = "员工姓名") @PathVariable(required = false) String StaffName){

        Staff staff=staffService.selectStaffBy(Long.valueOf(StaffId),StaffJobId,StaffName);
        if(staff==null){
            return Result.fail("该员工不存在！");
        }
        return Result.ok(staff,"查询员工成功！");
    }*/

    @GetMapping("/find/allStaff/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有员工相关信息")
    public Result<PageInfo> getAllStaff(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<Staff> staffs=staffService.selectAllStaff();
        if(staffs.size()==0){
            return Result.fail("未查到任何数据！");
        }
        PageInfo pageInfo=new PageInfo(staffs);
        return Result.ok(pageInfo,"查询员工数据成功！");
    }

    @GetMapping("/find/allStaffCount")
    @ResponseBody
    @ApiOperation(value = "查询所有员工数量")
    public int getAllStaffCount(){
        int count=staffService.selectStaffCount();
        /*if(count==0){
            return Result.fail("未查到任何数据！");
        }*/
        return count;
    }

    @PostMapping("add/staff/{staff}")
    @ApiOperation(value = "添加员工")
    public Result<Integer> addStaff(@ApiParam(value = "员工实体") @RequestBody Staff staff){
        int rt=staffService.insertStaff(staff);
        if(rt==0){
            return Result.fail("新增员工数据失败！");
        }
        return Result.ok(rt,"新增员工数据成功！");
    }

    @PostMapping("add/staffType/{staffType}")
    @ApiOperation(value = "添加员工类型")
    public Result<Integer> addStaffType(@ApiParam(value = "员工类型实体") @RequestBody StaffType staffType){
        int rt=staffService.insertStaffType(staffType);
        if(rt==0){
            return Result.fail("新增员工类型数据失败！");
        }
        return Result.ok(rt,"新增员工类型数据成功！");
    }

    @PostMapping("add/staffPosition/{staffPosition}")
    @ApiOperation(value = "添加员工职位")
    public Result<Integer> addStaffPosition(@ApiParam(value = "员工职位实体") @RequestBody StaffPosition staffPosition){
        int rt=staffService.insertStaffPosition(staffPosition);
        if(rt==0){
            return Result.fail("新增员工职位数据失败！");
        }
        return Result.ok(rt,"新增员工职位数据成功！");
    }

    @PostMapping("add/staffTitle/{staffTitle}")
    @ApiOperation(value = "添加员工技术职称")
    public Result<Integer> addStaffTitle(@ApiParam(value = "员工类型实体") @RequestBody StaffTitle staffTitle){
        int rt=staffService.insertStaffTitle(staffTitle);
        if(rt==0){
            return Result.fail("新增员工技术职称数据失败！");
        }
        return Result.ok(rt,"新增员工技术职称数据成功！");
    }

    @GetMapping("find/allStaffType/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有员工类型")
    public Result<PageInfo> getAllStaffType(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<String> staffTypes=staffService.selectAllStaffType();
        if(staffTypes.size()==0){
            return Result.fail("未查到任何数据！");
        }
        PageInfo pageInfo=new PageInfo(staffTypes);
        return Result.ok(pageInfo,"查询员工类型数据成功！");
    }

    @GetMapping("find/allStaffPosition/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有员工职位")
    public Result<PageInfo> getAllStaffPosition(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<String> staffPositions=staffService.selectAllStaffPosition();
        if(staffPositions.size()==0){
            return Result.fail("未查到任何数据！");
        }
        PageInfo pageInfo=new PageInfo(staffPositions);
        return Result.ok(pageInfo,"查询员工职位数据成功！");
    }

    @GetMapping("find/allStaffTitle/{pageNum}")
    @ResponseBody
    @ApiOperation(value = "查询所有员工技术职称")
    public Result<PageInfo> getAllStaffTitle(@ApiParam(value = "分页页数") @PathVariable(required = false) int pageNum){
        PageHelper.startPage(pageNum,10);
        List<String> staffTitles=staffService.selectAllStaffTitle();
        if(staffTitles.size()==0){
            return Result.fail("未查到任何数据！");
        }
        PageInfo pageInfo=new PageInfo(staffTitles);
        return Result.ok(pageInfo,"查询员工技术职称数据成功！");
    }

    @PostMapping("edit/staff/{staff}")
    @ApiOperation(value = "修改员工信息")
    public Result<Integer> editStaff(@ApiParam(value = "员工实体") @RequestBody Staff staff){
        int rt=staffService.updateStaff(staff);
        if(rt==0){
            return Result.fail("修改员工数据失败！");
        }
        return Result.ok(rt,"修改员工数据成功！");
    }

    @PostMapping("edit/staffStatus/{staffId}/{staffStatus}")
    @ApiOperation(value = "修改员工状态")
    public Result<Integer> editStaffStatus(@ApiParam(value = "员工ID") @PathVariable Long staffId,
                                           @ApiParam(value = "员工ID") @PathVariable int staffStatus){
        if(staffStatus <= -1 || staffStatus>=4){
            return Result.fail("状态码超出预设范围，修改员工状态失败！");
        }
        int rt=staffService.updateStaffStatus(staffId,staffStatus);
        if(rt==0){
            return Result.fail("修改员工状态失败！");
        }
        return Result.ok(rt,"修改员工状态成功！");
    }


    @GetMapping("/find/typeByStaffId/{staffId}")
    @ResponseBody
    @ApiOperation(value = "根据员工Id查询员工类型")
    public Result<String> getTypeByStaffId(@ApiParam(value = "员工Id",required = true) @PathVariable Long staffId){

        String type=staffService.selectTypeByStaffId(staffId);
        if(type==null){
            return Result.fail("未查询到该员工员工类型！");
        }
        return Result.ok(type,"查询员工类型成功！");
    }

    @GetMapping("/find/positionByStaffId/{staffId}")
    @ResponseBody
    @ApiOperation(value = "根据员工Id查询员工职位")
    public Result<String> getPositionByStaffId(@ApiParam(value = "员工Id",required = true) @PathVariable Long staffId){

        String position=staffService.selectPositionByStaffId(staffId);
        if(position==null){
            return Result.fail("未查询到该员工员工职位！");
        }
        return Result.ok(position,"查询员工职位成功！");
    }

    @GetMapping("/find/titleByStaffId/{staffId}")
    @ResponseBody
    @ApiOperation(value = "根据员工Id查询员工技术职称")
    public Result<String> getTitleByStaffId(@ApiParam(value = "员工Id",required = true) @PathVariable Long staffId){

        String title=staffService.selectTitleByStaffId(staffId);
        if(title==null){
            return Result.fail("未查询到该员工技术职称！");
        }
        return Result.ok(title,"查询员工技术职称成功！");
    }



}
